package torlesse;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.jupiter.api.Test;

import java.util.List;

/**
 * @author ：torlesse
 * @date ：Created in 21:11 2021/11/28 0028
 * @description ：
 * @version: 1.0
 */
public class MyActivityTest {

    /**
     * 生成workflow数据库表
     */
    @Test
    public void creatTable(){
        ProcessEngine processEngine = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml").buildProcessEngine();
    }

    //部署流程
    //调用引擎,初始化
//    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    ProcessEngine processEngine = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml").buildProcessEngine();
    /**
     * 部署流程定义
     */
    @Test
    public void deployment() {
        Deployment deployment = processEngine.getRepositoryService()//与流程定义和部署相关的Service
                .createDeployment()//创建一个部署对象
                .name("helloword")//添加部署的名称
                .addClasspathResource("holiday.bpmn20.xml")//从classpath下加载资源，一次一个
                .deploy();//完成部署
        System.out.println("部署ID"+deployment.getId());//1
        System.out.println("部署名称"+deployment.getName());
    }

    /**
     *启动流程实例
     */
    @Test
    public void startProcessInstance() {
        String processDefinitionKey = "test";
        ProcessInstance processInstance = processEngine.getRuntimeService()//与正在执行的流程实例和执行对象相关的Service
                .startProcessInstanceByKey(processDefinitionKey);//根据Key值来查询流程,也可以根据ID
        System.out.println("流程实例ID:"+processInstance.getId());  //2501
        //对应数据库act_ru_execution
        System.out.println("流程定义ID:"+processInstance.getProcessDefinitionId()); //helloword:1:4
        //对应数据库act_re_deployment
    }

    /**
     * 查询当前个人的任务
     */
    @Test
    public void fingByPerson() {
        String assignee = "部门经理审批";
        List<Task> list = processEngine.getTaskService()//与正在执行的任务管理相关的Service
                .createTaskQuery()//创建任务查询对象
//                .taskAssignee(assignee)//指定个人任务查询，指定代理人
                .list();//以list形式记录对象
        if(list != null && list.size()>0) {
            for(Task task:list) {
                System.out.println("任务ID:"+task.getId());//2501
                System.out.println("任务名称："+task.getName());//提交申请
                System.out.println("任务的创建时间："+task.getCreateTime());//Wed Jun 06 18:12:15 CST 2018
                System.out.println("任务的代理人："+task.getAssignee());//张三
                System.out.println("流程实例ID："+task.getProcessInstanceId());//2501
                System.out.println("执行对象ID："+task.getExecutionId());//2501
                System.out.println("流程定义ID："+task.getProcessDefinitionId());//helloword:1:4
            }
        }
    }

    /**
     * 完成个人任务
     */
    @Test
    public void complete() {
        String taskId = "12508";
        processEngine.getTaskService()
                .complete(taskId );
        System.out.println("完成任务ID："+taskId);
    }
}

